Skip to content

Conversation

@s1gr1d
Copy link
Contributor

@s1gr1d s1gr1d commented May 22, 2025

Which problem is this PR solving?

This is an upstream fix for getsentry/sentry-javascript#16211

Instead of adding / to non-existing routes, it sets the attribute to undefined, which makes it clear it's a non-existing route and not the index route.


A tricky thing is that the layers look the same if a non-existing route is requested or the root is requested: ['/', '/'].

  • /existing --> ['/', '/existing']
  • /non-existing --> ['/', '/']
  • / --> ['/', '/']

So in order to know if it was a 404 route, a utility function that does some additional checking was added.

@s1gr1d s1gr1d requested a review from a team as a code owner May 22, 2025 11:52
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented May 22, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@github-actions github-actions bot requested review from JamieDanielson and pkanal May 22, 2025 11:52
@pichlermarc pichlermarc added bug Something isn't working priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect labels May 22, 2025
pichlermarc
pichlermarc previously approved these changes May 22, 2025
Copy link
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! thank you for your contribution @s1gr1d 🙂

cc @raphael-theriault-swi, @JamieDanielson, @pkanal (component owners)

@pichlermarc
Copy link
Member

oh - looks like there are some lint errors, you can fix them by running npm run lint:fix and then pushing the resulting diff 🙂

@codecov
Copy link

codecov bot commented May 22, 2025

Codecov Report

Attention: Patch coverage is 97.67442% with 1 line in your changes missing coverage. Please review.

Project coverage is 89.72%. Comparing base (d6e7fe7) to head (2b9dd88).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...opentelemetry-instrumentation-express/src/utils.ts 97.43% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2843      +/-   ##
==========================================
- Coverage   97.62%   89.72%   -7.91%     
==========================================
  Files           9      187     +178     
  Lines         505     9097    +8592     
  Branches       89     1879    +1790     
==========================================
+ Hits          493     8162    +7669     
- Misses         12      935     +923     
Files with missing lines Coverage Δ
...try-instrumentation-express/src/instrumentation.ts 96.73% <100.00%> (ø)
...opentelemetry-instrumentation-express/src/utils.ts 97.22% <97.43%> (ø)

... and 176 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pichlermarc pichlermarc dismissed their stale review May 22, 2025 14:25

failing tests

@raphael-theriault-swi raphael-theriault-swi self-requested a review May 22, 2025 17:19
@s1gr1d
Copy link
Contributor Author

s1gr1d commented May 23, 2025

I have to look into this again. I see another test is failing. It doesn't add http.route either when the route is / (but this should be added in this case).

@s1gr1d
Copy link
Contributor Author

s1gr1d commented May 23, 2025

The problem was that the layers look the same if a non-existing route is requested or the root is requested: ['/', '/'].

So in order to know if it was a 404 route, I added a utility function that does some additional checking. I made sure that the existing tests still work and added some more tests for the utility function.

Copy link
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @s1gr1d, thanks for following up on this.

It seems that this PR is based on a commit that's a little over 2 months old. Would you mind rebasing it on top of main it so that we can see how this would fit in with the current state? 🙂

@s1gr1d s1gr1d force-pushed the sigrid/handle-express-404 branch from 6592b20 to 99a94d2 Compare May 23, 2025 12:52
@s1gr1d s1gr1d requested a review from pichlermarc May 23, 2025 12:53
@s1gr1d s1gr1d marked this pull request as draft May 26, 2025 13:09
@s1gr1d s1gr1d force-pushed the sigrid/handle-express-404 branch from 99a94d2 to 006539c Compare June 3, 2025 12:47
@s1gr1d s1gr1d marked this pull request as ready for review June 3, 2025 12:56
Comment on lines 271 to 275
if (constructedRoute.includes('/') &&
(constructedRoute.includes(',') ||
constructedRoute.includes('\\') ||
constructedRoute.includes('*') ||
constructedRoute.includes('['))) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition is for considering regex routes (without this, the regex tests would fail). This should cover most cases.

There is a previous conversation about this: #2008 (comment)

@s1gr1d
Copy link
Contributor Author

s1gr1d commented Jun 4, 2025

@pichlermarc I rebased it and aligned the code so the "regex" tests don't fail anymore.

Copy link
Contributor

@david-luna david-luna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :) just a couple of nits

Comment on lines +221 to +223
const layersStore: string[] = Array.isArray(req[_LAYERS_STORE_PROPERTY])
? (req[_LAYERS_STORE_PROPERTY] as string[])
: [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: _LAYERS_STORE_PROPERTY is set only in storeLayerPath function. Meaning that if defined it must be an array. So this const assignment could be simplified to.

const layersStore = req[_LAYERS_STORE_PROPERTY] || [];

I see you added a test specifically for a scenario where that property is defined with a non-array value. Probably to avoid a case where another piece of code outside the instrumentation modifies the property. If you want the extra safety of Array.isArray it's okay but there is no need to cast types. TS resolves the type properly from the function signature.

const layersStore = Array.isArray(req[_LAYERS_STORE_PROPERTY])
    ? req[_LAYERS_STORE_PROPERTY]
    : [];

NOTE: maybe if the instrumentation uses a Symbol instead of a string it will ensure no other code could change the layer store

originalUrl: PatchedRequest['originalUrl'];
[_LAYERS_STORE_PROPERTY]?: string[];
}): string | undefined {
const layersStore: string[] = Array.isArray(req[_LAYERS_STORE_PROPERTY])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: same here

@pichlermarc pichlermarc enabled auto-merge (squash) June 13, 2025 09:00
@pichlermarc pichlermarc merged commit 85f6398 into open-telemetry:main Jun 13, 2025
23 checks passed
@dyladan dyladan mentioned this pull request Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working pkg:instrumentation-express priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants